Passed
Push — master ( 801361...7f4db7 )
by Dongxin
49s
created

blog-encrypt.js ➔ decryptAES   B

Complexity

Conditions 4
Paths 93

Size

Total Lines 83
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 42
c 3
b 0
f 0
nc 93
nop 0
dl 0
loc 83
rs 8.872

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
'use strict';
2
3
function decryptAES () {
4
5
  const pass = String(document.getElementById('pass').value);
6
  try {
7
8
    var decryptionError = String(document.getElementById('decryptionError').innerHTML);
9
    var noContentError = String(document.getElementById('noContentError').innerHTML);
10
11
  } catch (e) {
12
13
    decryptionError = 'Incorrect Password!';
14
    noContentError = 'No content to display!';
15
16
  }
17
18
  try {
19
20
    let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), pass);
0 ignored issues
show
Bug introduced by
The variable CryptoJS seems to be never declared. If this is a global, consider adding a /** global: CryptoJS */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
21
    content = content.toString(CryptoJS.enc.Utf8);
22
    content = decodeBase64(content);
23
    content = unescape(content);
24
    if (content === '') {
25
26
      throw new Error(noContentError); // ???
27
28
    } else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
29
30
      document.getElementById('encrypt-blog').style.display = 'inline';
31
      document.getElementById('encrypt-blog').innerHTML = '';
32
33
      // Use jquery to load some js code
34
      try {
35
36
        $('#encrypt-blog').html(content);
37
38
        {callback}
0 ignored issues
show
Bug introduced by
The variable callback seems to be never declared. If this is a global, consider adding a /** global: callback */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
39
40
      } catch(e) {
41
42
        const errorInfo = '<p>'
43
                      + 'Some errors occurred, check the original file please.'
44
                      + 'Detailed exception are shown in console.'
45
                      + '</p>';
46
        console.error(e);
47
        $('#encrypt-blog').html(errorInfo);
48
49
      }
50
51
      document.getElementById('hbe-security').style.display = 'none';
52
      if (document.getElementById('toc-div')) {
53
54
        document.getElementById('toc-div').style.display = 'inline';
55
56
      }
57
58
    }
59
    // Call MathJax to render
60
    if(typeof MathJax !== 'undefined') {
0 ignored issues
show
Bug introduced by
The variable MathJax seems to be never declared. If this is a global, consider adding a /** global: MathJax */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
61
62
      try {
63
64
        MathJax.Hub.Queue(
65
          ['resetEquationNumbers', MathJax.InputJax.TeX, ],
66
          ['PreProcess', MathJax.Hub, ],
67
          ['Reprocess', MathJax.Hub, ]
68
        );
69
70
      } catch (e) {
71
72
        console.log('Can\'t render with MathJax');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
73
74
      }
75
76
    }
77
78
  } catch (e) {
79
80
    alert(decryptionError);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
81
    console.log(e);
82
83
  }
84
85
}
86
87
function htmlDecode (str) {
88
89
  let s = '';
90
  if (str.length == 0) {
0 ignored issues
show
Best Practice introduced by
Comparing str.length to 0 using the == operator is not safe. Consider using === instead.
Loading history...
91
92
    return '';
93
94
  }
95
96
  s = str.replace(/&gt;/g, '&');
97
  s = s.replace(/&lt;/g, '<');
98
  s = s.replace(/&gt;/g, '>');
99
  s = s.replace(/&nbsp;/g, '    '); // ??? why not ' '
100
  s = s.replace(/'/g, '\'');
101
  s = s.replace(/&quot;/g, '"');
102
  s = s.replace(/<br>/g, '\n');
103
  return s;
104
105
}
106
107
function decodeBase64 (content) {
108
109
  content = CryptoJS.enc.Base64.parse(content);
0 ignored issues
show
Bug introduced by
The variable CryptoJS seems to be never declared. If this is a global, consider adding a /** global: CryptoJS */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
110
  content = CryptoJS.enc.Utf8.stringify(content);
111
  return content;
112
113
}
114
115
// Since you decided to use jQuery.
116
$(document).ready(
117
  function () {
118
119
    console.log('Registering Enter for decrypt.');
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
120
    document.getElementById('pass').onkeypress = function (keyPressEvent) {
121
122
      if (keyPressEvent.keyCode === 13) {
123
124
        decryptAES();
125
126
      }
127
128
    };
129
130
  }
131
);
132